/*
   Copyright 2009 Bégaudeau Stéphane

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package faneclipse.wizards;

import org.eclipse.core.resources.IFile;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;

import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;

import org.eclipse.ui.ide.IDE;

/**
 * @author BEGAUDEAU stephane
 */
public class FanFileWizard extends Wizard implements INewWizard {
	/**
	 * the page of the wizard.
	 */
	private FanFirstPageFileWizard firstPage;
	
	/**
	 * the selection.
	 */
	private IStructuredSelection selection;
    
	/**
	 * the workbench.
	 */
	private IWorkbench workbench;
    
    /**
     * The constructor.
     */
    public FanFileWizard() {
    	setWindowTitle("New Fan File");
    } 
    
    /**
     * @see org.eclipse.jface.wizard.Wizard#addPages()
     */
    @Override
    public final void addPages() {
    	firstPage = new FanFirstPageFileWizard(selection);
    	addPage(firstPage);
    }
    
	/**
	 * @return A boolean showing if we can finish the action of the wizard.
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
	@Override
	public final boolean performFinish() {
		IFile file = firstPage.createNewFile();
        if (file != null)
        {
        	IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
        	
        	try
        	{
                IDE.openEditor(page, file);
            }
        	catch (PartInitException e)
        	{
        		e.printStackTrace();
            }
        	
            return true;
        }
        else
        {
            return false;
        }
	}

	/**
	 * @param workbench The workbench.
	 * @param selection The selection.
	 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
	 */
	public final void init(final IWorkbench workbench, final IStructuredSelection selection) {
		this.workbench = workbench;
		this.selection = selection;
	}

}
